﻿function loadProduct() {
    var arr = [];
    var countPhone = 0;
    var countPad = 0;
    var countWear = 0;
    var countShow = 0;
    if ($.cookie('phone') != null) {
        arr = $.merge(arr, $.cookie('phone').split(','));
        countPhone = $.cookie('phone').split(',').length;
    }
    if ($.cookie('pad') != null) {
        arr = $.merge(arr, $.cookie('pad').split(','));
        countPad = $.cookie('pad').split(',').length;
    }
    if ($.cookie('wear') != null) {
        arr = $.merge(arr, $.cookie('wear').split(','));
        countWear = $.cookie('wear').split(',').length;
    }
    var count = arr.length;
    arr = arr.toString();
    if (count > 0) {
        addItem(arr);
        if (countPhone > countPad && countPhone > countWear) {
            countShow = countPhone;
            showComparePhone();
        }
        else if (countPad > countPhone && countPad > countWear) {
            countShow = countPad;
            showComparePhone();
        }
        else{
            countWear = countWear;
            showCompareWear();
        }
        if (countShow >= 5) { hideCompareEmpty(); hideProposeContainer(); }
            else { showCompareEmpty(); }
    }
    
    getRefer(1);
}

function addItem(arr) {
    $.get("Handler/HandlerCompare.ashx",
       { status: 'getProductsInfo', products: arr },
       function (data) {
           var count = data.length;
           for (var i = 0; i < count; i++) {

               var ID = data[i].ID;
               var Image = "images/iphone.png";
               var Name = data[i].Name;
               var Size = data[i].Size;
               var Weight = data[i].Weight;
               var BatteryTalkTime = data[i].BatteryTalkTime;
               var BatteryCapacity = data[i].BatteryCapacity;
               var Color = data[i].Color;
               var Price = data[i].Price;
               var CategoryID = data[i].CategoryID;
               var Brand = data[i].Brand;
               var Connect = data[i].Connect;
               var OperatingSystem = data[i].OperatingSystem;
               var ProcessorCore = data[i].ProcessorCore;
               var ProcessorSpeed = data[i].ProcessorSpeed;
               var ScreenSize = data[i].ScreenSize;
               var ScreenResolution = data[i].ScreenResolution;
               var CameraResolutionRear = data[i].CameraResolutionRear;
               var CameraResolutionFront = data[i].CameraResolutionFront;
               var Ram = data[i].Ram;
               var Rom = data[i].Rom;
               if (data[i].Image != null) {
                   Image = "data:image/jpg;base64," + data[i].Image;
               }

               var item = '<div class="compare-item-container" style="display:none">' +
                                  '<div class="compare-item-prop">' +
                                  '<div class="' + ID + '">' +
                                  '<img class="' + CategoryID + '" src="images/icon-cancel.png"/>' +
                                  '</div>' +
                                  '</div>' +
                                  '<div class="compare-item-content">' +
                                  '<ul>' +
                                  '<li>' +
                                  '<div class="compare-img">' +
                                  '<img  src="' + Image + '" />' +
                                  '</div>' +
                                  '</li>' +
                                  '<li class="pro-nm">' + Name + '</li>' +
                                  '<li class="pro-brand">' + Brand + '</li>' +
                                  '<li class="pro-color">' + Color + '</li>' +
                                  '<li class="pro-size">' + Size + '</li>' +
                                  '<li class="pro-weight">' + Weight + '</li>' +
                                  '<li class="pro-rom">' + Rom + '</li>' +
                                  '<li class="pro-ram">' + Ram + '</li>' +
                                  '<li class="pro-core">' + ProcessorCore + '</li>' +
                                  '<li class="pro-speed">' + ProcessorSpeed + '</li>' +
                                  '<li class="pro-screensize">' + ScreenSize + '</li>' +
                                  '<li class="pro-screenresolution">' + ScreenResolution + '</li>' +
                                  '<li class="pro-rearcam">' + CameraResolutionRear + '</li>' +
                                  '<li class="pro-frontcam">' + CameraResolutionFront + '</li>' +
                                  '<li class="pro-battercapacity">' + BatteryCapacity + '</li>' +
                                  '<li class="pro-talktime">' + BatteryTalkTime + '</li>' +
                                  '<li class="pro-os">' + OperatingSystem + '</li>' +
                                  '<li class="pro-connstandard">' + Connect + '</li>' +
                                  '<li class="pro-price">' + Price + '</li>' +
                                  '</ul>' +
                                  '</div>' +
                                  '</div>'

               var CompareType;

               switch (CategoryID) {
                   case 1:
                       CompareType = ".compare-phone";
                       break;
                   case 2:
                       CompareType = ".compare-pad";
                       break;
                   case 3:
                       CompareType = ".compare-wear";
                       break;
               };
               $(CompareType).append(item);
               showElement($('.compare-item-container'));
           }
       });
}

function deleteItem(elem) {
    var $this = elem;
    var ID = $this.children('div').attr('class');
    var CategoryID = parseInt($this.find('img').attr('class'));
    $this.parent('.compare-item-container').remove();
    var cookieType = checkCookieType(CategoryID);
    var arr = [];
    if ($.cookie(cookieType) != null) {
        arr = $.cookie(cookieType).split(',');
    }
    if (arr.length == 1) {
        $.removeCookie(cookieType);
        showCompareEmpty();
        hideProposeContainer();
    }
    else {
        arr = jQuery.grep(arr, function (value) {
            return value != ID;
        });
        $.cookie(cookieType, arr);
        getRefer(CategoryID);
    }
}

function showComparePhone() {
    $('.cate-pad, .cate-wear').addClass('cate-hover');
    $('.cate-phone').removeClass('cate-hover');
    $('.cate-phone>img').attr("src", "images/boxicon-phone2.png");
    $('.cate-pad>img').attr("src", "images/boxicon-pad.png");
    $('.cate-wear>img').attr("src", "images/boxicon-wear.png");
    $('.compare-pad, .compare-wear, .compare-item-empty').hide();
    showElement($('.compare-phone, .compare-item-empty'));
}

function showComparePad() {
    $('.cate-phone, .cate-wear').addClass('cate-hover');
    $('.cate-pad').removeClass('cate-hover');
    $('.cate-phone>img').attr("src", "images/boxicon-phone.png");
    $('.cate-pad>img').attr("src", "images/boxicon-pad2.png");
    $('.cate-wear>img').attr("src", "images/boxicon-wear.png");
    $('.compare-phone, .compare-wear, .compare-item-empty').hide();
    showElement($('.compare-pad, .compare-item-empty'));
}

function showCompareWear() {
    $('.cate-phone, .cate-pad').addClass('cate-hover');
    $('.cate-wear').removeClass('cate-hover');
    $('.cate-phone>img').attr("src", "images/boxicon-phone.png");
    $('.cate-pad>img').attr("src", "images/boxicon-pad.png");
    $('.cate-wear>img').attr("src", "images/boxicon-wear2.png");
    $('.compare-phone, .compare-pad, .compare-item-empty').hide();
    showElement($('.compare-wear, .compare-item-empty'));
}



function showCompareEmpty() {
    $('.compare-item-empty').show();
}

function hideCompareEmpty() {
    $('.compare-item-empty').hide();
}

function showProposeContainer() {
    showElement($('.propose-container'));
}

function hideProposeContainer() {
    $('.propose-container').hide();
}

function sortList(arr) {
    arr.sort(function (a, b) { return a - b });
    return arr;
}

function getRefer(CategoryID) {
    $.cookie('c_t', CategoryID); //compare_type
    CategoryID = parseInt(CategoryID);
    var arr = [];
    switch (CategoryID) {
        case 1:
            if ($.cookie('phone') != null) {
                arr = $.cookie('phone').split(',');
            }
            break;
        case 2:
            if ($.cookie('pad') != null) {
                arr = $.cookie('pad').split(',');
            }
            break;
        case 3:
            if ($.cookie('wear') != null) {
                arr = $.cookie('wear').split(',');
            }
            break;
    }
    arr = sortList(arr);
    var count = arr.length;
    if (count >= 5) { hideCompareEmpty(); }
    else { showCompareEmpty(); }

    if (count == 0 || count == 5) {
        hideProposeContainer();
    }
    else {
        arr = arr.toString();
        $.get("Handler/HandlerCompare.ashx",
        { status: 'getRefer', products: arr },
        function (data) {
            showProposeContainer();
            $('.propose-container').empty();
            for (var i = 0; i < data.length; i++) {
                var Image = 'images/defaultPhone.png';
                var Name = data[i].Name.substr(0, 30);
                var ID = data[i].ID;
                var CategoryID = data[i].CategoryID;
                if (data[i].Image != null) {
                    Image = "data:image/jpg;base64," + data[i].Image;
                }
                var item = '<div class="propose-item-container">' +
                                    '<div class="propose-item">' +
                                    '<div class="propose-item-img">' +
                                    '<img src="' + Image + '" />' +
                                    '</div></div>' +
                                    '<div class="' + ID + '">' +
                                    '<span class="' + CategoryID + '">' + Name + '</span>' +
                                    '</div></div>';
                $('.propose-container').append(item);
            }
        })
    }
};

function compareChange(elem) {
    switch (elem.attr('class').split(' ')[0]) {
        case 'cate-phone':
            showComparePhone();
            getRefer(1);
            break;
        case 'cate-pad':
            showComparePad();
            getRefer(2);
            break;
        case 'cate-wear':
            showCompareWear();
            getRefer(3);
            break;
    }
}

function checkCookieType(CategoryID) {
    CategoryID = parseInt(CategoryID);
    var cookieType;
    switch (CategoryID) {
        case 1:
            cookieType = 'phone';
            break;
        case 2:
            cookieType = 'pad';
            break;
        case 3:
            cookieType = 'wear';
            break;
    }
    return cookieType;
}

function proposeItemClick(elem) {
    var $this = elem;
    var ID = $this.children('div').eq(1).attr('class');
    var CategoryID = $this.children('div').eq(1).children('span').attr('class');
    var cookieType = checkCookieType(CategoryID);
    var arr = [];
    if ($.cookie(cookieType) != null) {
        arr = $.cookie(cookieType).split(',');
    }

    if ($.inArray(ID, arr) == -1) {
        arr = jQuery.grep(arr, function (value) {
            return value != ID;
        });
        arr.push(ID);
        $.cookie(cookieType, arr);
        //alert('已加入比拚');
        addItem(ID);
        arr = arr.toString();
        saveCompareRecord(arr, CategoryID);
    }
}

function saveCompareRecord(arr, cateID) {
    $.get("Handler/HandlerCompare.ashx",
    { status: 'saveCompareRecord', products: arr });
    if (arr.split(',').length >= 5) {
        hideCompareEmpty();
        hideProposeContainer();
    }
    else {
        getRefer(cateID);
        showCompareEmpty();
        showProposeContainer();
    }
}